﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CatSentenceAnalyzer
{
    public class CATEnSeg : CLangSegment
    {
        private string m_sregx = @"[a-z]+";
        private int m_engminlen = 2;

        public CATEnSeg(){
            m_type = 1;
        }

        override public int GetSentenceSumKey()
        {
            string tmp = ""; int sum = 0;
            GetRegexCollection(m_sregx);
            SumKeyWordLength = 0;
            for (int i = 0; i < m_sentencecoll.Count; i++)
            {

                if (m_sentencecoll[i].Groups.Count > 0)
                {
                    SumKeyWordLength++;
                    tmp += m_sentencecoll[i].Groups[m_sentencecoll[i].Groups.Count - 1].Value;
                }
            }
            sum = GetSumKey(tmp);
            if (sum == 0)
            {
                sum = SumKey(tmp);
                putHashKey(sum.ToString());
            }
            else
            {
                putKey(sum);
            }
            return sum;
        }



        override public ArrayList GetMatchCollection3()
        {
            int j;
            if (m_sentencecoll == null)
                GetRegexCollection();
            for (int i = 0; i < m_sentencecoll.Count; i++)
            {
                string tmp = m_sentencecoll[i].Groups[m_sentencecoll[i].Groups.Count - 1].Value;
                MatchCollection mc = GetRegx(m_sregx, tmp);
                int ilen = mc.Count;
                if (ilen >= MINLEN * 2)
                {
                    string s1="", s2="", s3="";
                    int mod = ilen % 3;
                    if (mod == 0)//偶数
                    {
                        mod = ilen / 3;
                        for (j = 0; j < mod; j++) s1 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        for (j = mod; j < mod*2; j++) s2 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        for (j = mod*2; j < ilen; j++) s3 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        putKey(GetSumKey(s1));
                        putKey(GetSumKey(s2));
                        putKey(GetSumKey(s3));

                    }
                    else
                    {//奇数
                        mod = ilen / 3;
                        for (j = 0; j < mod; j++) s1 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        for (j = mod; j < mod * 2; j++) s2 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        for (j = mod * 2; j < ilen; j++) s3 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        putKey(GetSumKey(s1));
                        putKey(GetSumKey(s2));
                        putKey(GetSumKey(s3));

                        mod = ilen / 3;
                        for (j = 0; j < mod+1; j++) s1 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        for (j = mod+1; j < mod * 2+1; j++) s2 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        for (j = mod * 2+1; j < ilen; j++) s3 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        putKey(GetSumKey(s1));
                        putKey(GetSumKey(s2));
                        putKey(GetSumKey(s3));
                    }
                }
                else
                {
                    tmp = "";
                    for (j = 0; j < mc.Count; j++)
                    {
                        string s = mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        if (s.Length > m_engminlen)
                            putKey(GetSumKey(s));
                    }
                }
            }
            return m_MatchSumKey;
        }

        override public ArrayList GetMatchCollection2()
        {
            int j;
            if (m_sentencecoll == null)
                GetRegexCollection();
            for (int i = 0; i < m_sentencecoll.Count; i++)
            {
                string tmp = m_sentencecoll[i].Groups[m_sentencecoll[i].Groups.Count - 1].Value;
                MatchCollection mc = GetRegx(m_sregx, tmp);
                int ilen = mc.Count;
                if (ilen > MINLEN)
                {
                    string s1 = "", s2 ="";
                    int mod = ilen % 2;
                    if (mod == 0)//偶数
                    {
                        mod = ilen / 2;
                        for (j = 0; j < mod; j++) s1 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        for (j = mod; j < ilen; j++) s2 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        putKey(GetSumKey(s1));
                        putKey(GetSumKey(s2));
                    }
                    else
                    {//奇数
                        mod = ilen / 2;
                        for (j = 0; j < mod; j++) s1 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        for (j = mod; j < ilen; j++) s2 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        putKey(GetSumKey(s1));
                        putKey(GetSumKey(s2));
                        for (j = 0; j < mod+1; j++) s1 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        for (j = mod+1; j < ilen; j++) s2 += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        putKey(GetSumKey(s1));
                        putKey(GetSumKey(s2));
                    }
                }
                else
                {
                    tmp = "";
                    for ( j = 0; j < mc.Count; j++)
                    {
                        string s = mc[j].Groups[mc[j].Groups.Count - 1].Value;
                        if( s.Length >m_engminlen)
                            putKey(GetSumKey(s));
                    }
                   
                }
            }
            return m_MatchSumKey;
        }

        override public ArrayList GetMatchCollection1()
        {
            GetRegexCollection();
            for (int i = 0; i < m_sentencecoll.Count; i++)
            {
                string tmp = "";
                MatchCollection mc = GetRegx(m_sregx , m_sentencecoll[i].Groups[m_sentencecoll[i].Groups.Count-1].Value);
                for (int j = 0; j < mc.Count; j++)
                {
                    tmp += mc[j].Groups[mc[j].Groups.Count - 1].Value;
                }
                int sum = GetSumKey(tmp);
                putKey(sum);
            }
            return m_MatchSumKey;
        }

        override public int GetDiectoryIndexKey()
        {
            string tmp = ""; int sum = 0;
            if (m_sentencecoll == null)
                GetRegexCollection(m_sregx);

            if (m_sentencecoll.Count >0)
            {
                tmp = m_sentencecoll[0].Groups[m_sentencecoll[0].Groups.Count - 1].Value;
                sum = GetSumKey(tmp);
                putKey(sum);
            }
            return sum;
        }

        override public ArrayList GetTdbCollection()
        {
            string tmp = "";
            //if (m_sentencecoll == null)
                GetRegexCollection(m_sregx);
            for (int i = 0; i < m_sentencecoll.Count; i++)
            {
                tmp =  m_sentencecoll[i].Groups[0].Value;
                putKey(GetSumKey(tmp));
            }
            return m_MatchList;
        }

        //public ArrayList GetTdbCollection1()//分句处理
        //{
        //    ArrayList list = new ArrayList();
        //    if (m_sentencecoll == null)
        //        GetRegexCollection();
        //    for (int i = 0; i < m_sentencecoll.Count; i++)
        //    {
        //        MatchCollection mc = GetRegx(m_sregx, m_sentencecoll[i].Groups[m_sentencecoll[i].Groups.Count - 1].Value);
        //        string[] arr = new string[mc.Count];
        //        for (int j = 0; j < mc.Count; j++)
        //        {
        //            arr[j] = mc[j].Groups[mc[j].Groups.Count - 1].Value;
        //        }
        //        list.Add(arr);
        //    }
        //    return list;
        //}
    }
}
